package com.yoomiito.app.ui.yijiayou

import android.Manifest
import android.content.Context
import android.content.Intent
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.LinearLayout
import com.yoomiito.app.*
import com.yoomiito.app.base.WebViewActivity
import com.yoomiito.app.model.LatLng
import com.yoomiito.app.model.oilcard.OilCardData
import com.yoomiito.app.ui.capture.CaptureActivity
import com.yoomiito.app.ui.goods.NewGoodsInfoActivity
import com.yoomiito.app.utils.*
import com.yoomiito.app.utils.encrypt.AESUtils
import com.yoomiito.app.widget.dialog.TipDialog
import kotlinx.android.synthetic.main.view_oil_card.view.*

/**
 *
 */
class OilCardView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0)
    : LinearLayout(context, attrs, defStyleAttr, defStyleRes) {
    var saoOnClickListener:(Int)->Unit = {}
    private val view = LayoutInflater.from(context).inflate(R.layout.view_oil_card, null, false)
    private lateinit var data: OilCardData
    private val plat = BuildConfig.yjyp
    private val app_key = BuildConfig.yjyak
    private val app_secret =BuildConfig.yjyas
    companion object{
        val SaoYiSao = 1
        val BuyOilCard = 2
        const val OilCardGoodsId = "5230629252506342"
    }
    init {
        val lp = LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        addView(view, lp)
        money.typeface = AllUtils.getTypeface()
        times.typeface = AllUtils.getTypeface()
        saveMoney.typeface = AllUtils.getTypeface()
        payOilCard.setOnClickListener { saoOnClickListener(BuyOilCard) }
        sao.setOnClickListener { saoOnClickListener(SaoYiSao)  }
//        goOil.setOnClickListener {
////            if (data.latitude.compareTo(-1)==0||data.longitude.compareTo(-1)==0){
////                //未定位
//            if (SpannableUtils.isEmptyOr0(data.money)){
//                "加油卡余额为0，请充值".showToast()
//            }else{
//                reLocation()
//            }
//        }
//        goOil.isSelected = false
    }

    fun addOil(){
//        val timestamp = System.currentTimeMillis()
//        val sign = AESUtils.MD5("${app_key}#${timestamp}#${app_secret}")
//        val url = "${BuildConfig.YiJiaYouUrl}?plat=${plat}&userPhone=${UserInfoConfig.getUser().mobile}&sign=$sign&timestamp=${timestamp}&latitude=${data.latitude}&longitude=${data.longitude}"
//        val intent = Intent(context, WebViewActivity::class.java)
//        intent.putExtra(Constant.TYPE, Const.Url.YiJiaYou)
//        intent.putExtra(Constant.DATA, url)
//        context.startActivity(intent)
        GasStationListActivity.start(context, LatLng(data.latitude,data.longitude))
    }

    fun buyOilCardReuest(){
        if (this::data.isInitialized){
            showOilRuleDialog(data.address)
        }
    }

    fun updateData(data: OilCardData) {
        this.data = data
        money.text = data.money.formatNum1()
        times.text = data.nums.formatNum2()
        saveMoney.text = data.totalDiscount.formatNum1()
//        goOil.isSelected = !SpannableUtils.isEmptyOr0(data.money)
    }

    fun reset(){
        money.text = "0"
        times.text= "0"
        saveMoney.text = "0"
    }

    private fun showOilRuleDialog(data: String) {
        if (data.isEmpty()) {
            buyOilCard()
        } else {
            val tipDialog = TipDialog(context)
            tipDialog.setCancelText("取消")
            tipDialog.setContent(data)
            tipDialog.setSureText("确定充值")
            tipDialog.setTitle("加油卡使用范围")
            tipDialog.setTitleVisible(true)
            tipDialog.setClickListener {
                tipDialog.dismiss()
                buyOilCard()
            }
            tipDialog.show()
        }
    }

    private fun buyOilCard() {
        NewGoodsInfoActivity.toGoodsInfo(context,OilCardGoodsId , Type.ServiceConfigGoodsType.GoodsMainTypePrivateGoods.toString())
    }

}